package com.my.quartz.boot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.quartz.boot.ScheduleUtils;
import com.my.quartz.boot.pojo.SysJob;
import com.my.quartz.boot.TaskException;
import com.my.quartz.boot.mapper.SysJobMapper;
import com.my.quartz.boot.service.SysJobService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Auto Injected
 *
 * @author ：Boy
 * @date ：Created in 2022-06-18
 * @description：
 * @modified By：
 */
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements SysJobService {
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ScheduleUtils scheduleUtils;

    /**
     * 项目启动时，初始化定时器
     * 主要是防止手动修改数据库导致未同步到定时任务处理（注：不能手动修改数据库ID和任务组名，否则会导致脏数据）
     */
    @PostConstruct
    public void init() throws SchedulerException, TaskException {
        scheduler.clear();
        List<SysJob> jobList = baseMapper.selectList(null);
        for (SysJob job : jobList) {
            scheduleUtils.createScheduleJob(scheduler, job);
        }
    }
}
